const app = getApp();

const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}
//客服聊天界面
function gotoServiceTalk(e){
  wx.navigateTo({
    url: '../talk/talk'
  })
}
/**我的信息界面*/
function gotoMyInfo(e){
  wx.navigateTo({
    url: '../myinfo/myinfo?name=jason',
    events: {
      //data:{"name":"我是谁"},
      // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
      acceptDataFromOpenedPage: function(data) {
        console.log("indexPage:"+data)
      },
      someEvent: function(data) {
        console.log(data)
      },
      success: function(res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', { data: 'test' })
      }
    },
  })
}
//注册会员界面
function gotoMyRegister(e){
  wx.navigateTo({
    url: '../myRegister/myRegister'
  })
}
//单个景区界面
function gotoScenic(e){
  /**var str = ""
  for(var key in e){
    str += "" + key + "=" + e[key]
    str += "&"
  }
  str = str.substring(0, str.length-1)**/
  var str = JSON.stringify(e);//
  wx.navigateTo({
    url: '../scenic/scenic?data='+str
  })
}
//所有景区列表界面
function gotoScenicList(e){
  wx.navigateTo({
    url: '../scenicList/scenicList'
  })
}
//门票列表界面
function gotoTicketsList(e){
  wx.navigateTo({
    url: '../ticketsList/ticketsList'
  })
}
//松阳礼物页面
function gotoGiftPage(e){
  wx.navigateTo({
    url: '../gift/gift'
  })
}
//民宿酒店预订列表页面
function gotoHotelsListPage(e){
  // wx.navigateTo({
  //   url: '../hotelsList/hotelsList'
  // })
  wx.navigateTo({
    url: '../hotelTempList/hotelTempList'
  })
}
//民宿酒店-界面
function gotoHotelDetails(e){
  var str = JSON.stringify(e);//
  wx.navigateTo({
    url: '../hotelDetails/hotelDetails?data='+str
  })
}
//民宿酒店-预定页面
function gotoHotelBuyPage(e){
  wx.navigateTo({
    url: '../hotelBuy/hotelBuy?data='+e
  })
}

//餐饮预订、热门列表页面
function gotoResttListPage(e){
  wx.navigateTo({
    url: '../resttList/resttList?type='+e
  })
}
//餐饮页面
function gotoResttPage(e){
  wx.navigateTo({
    url: '../restt/restt?type='+e
  })
}
//评价建议页面
function gotoAppreisePage(e){
  wx.navigateTo({
    url: '../appreise/appreise'
  })
}
//评价建议页面-我的评价
function gotoCommentMePage(e){
  wx.navigateTo({
    url: '../commentMe/commentMe'
  })
}
//评价建议页面-发表评价
function gotoCommentSubmitPage(e){
  wx.navigateTo({
    url: '../commentSubmit/commentSubmit'
  })
}
//游玩攻略页面
function gotoPlayGuidePage(e){
  wx.navigateTo({
    url: '../playGuide/playGuide'
  })
}
//全部商品页面
function gotoShopListPage(e){
  e = JSON.stringify(e);
  wx.navigateTo({
    url: '../shopList/shopList?tabs='+e
  })
}
//具体单个门票购买页面
function gotoTicketsBuyPage(e){
  wx.navigateTo({
    url: '../ticketsBuy/ticketsBuy?data='+e
  })
}
//地图导航页面
function gotoMapPage(e){
  wx.navigateTo({
    url: '../map/map?data='+e
  })
}
//商品订单提交页面-确认订单
function gotoShopSubmitPage(arr){
  arr = JSON.stringify(arr);
  wx.navigateTo({
    url: '../shopSubmit/shopSubmit?arr='+arr
  })
}
//订单记录-所有已下单，订单页面
function gotoOrderGoodsPage(type, data=""){
  wx.navigateTo({
    url: '../orderGoods/orderGoods?type='+type
  })
}
//订单记录-门票订单详情页面
function gotoOrderTicketsPage(data){
  wx.navigateTo({
    url: '../orderTickets/orderTickets?data='+data
  })
}
//订单记录-酒店订单详情页面
function gotoOrderHotelsPage(data){
  wx.navigateTo({
    url: '../orderHotels/orderHotels?data='+data
  })
}
//订单记录-商品订单详情页面
function gotoOrderShopsPage(data){
  wx.navigateTo({
    url: '../orderShops/orderShops?data='+data
  })
}
//商品详情页面
function gotoShopDetailsPage(e){
  e = JSON.stringify(e);
  wx.navigateTo({
    url: '../shopDetails/shopDetails?data='+e
  })
}
//商品快捷列表页面
function gotoShopQuickPage(e){
  wx.navigateTo({
    url: '../shopQuickList/shopQuickList?type='+e
  })
}
//商品-地址管理页面
function gotoShopAddressPage(){
  wx.navigateTo({
    url: '../shopAddress/shopAddress'
  })
}
//商品-取货人管理页面
function gotoShopConsigneePage(){
  wx.navigateTo({
    url: '../shopConsignee/shopConsignee'
  })
}
//景区介绍页面
function gotoScenicDetailsPage(e){
  wx.navigateTo({
    url: '../scenicDetails/scenicDetails?data='+e
  })
}
//组件测试便捷复制页面
function gotoTestPage(e){
  wx.navigateTo({
    url: '../atest/atest'
  })
}
function sortArray(arr, sortByStringKey, isDown) {
  if (sortByStringKey) {
    //console.log("排序测试：" + (""+2).localeCompare("1"));
    if(isDown){
      return arr.sort((a, b) => {
        if(a[sortByStringKey] > b[sortByStringKey]){
          return -1;
        }
        else if(a[sortByStringKey] < b[sortByStringKey]){
          return 1;
        }
        else{
          return 0;
        }
      });
    }
    else{
      return arr.sort((a, b) => {
        if(a[sortByStringKey] < b[sortByStringKey]){
          return -1;
        }
        else if(a[sortByStringKey] > b[sortByStringKey]){
          return 1;
        }
        else{
          return 0;
        }
      });
    }
  } else {
    return arr.sort((a, b) => a - b);
  }
}
function getDateStr(time){
  var date = new Date(time);
  var year = date.getFullYear();
  if(year < 10){ year = "0"+year; }
  var month = date.getMonth() + 1;
  if(month < 10){ month = "0"+month; }
  var day = date.getDate();
  if(day < 10){ day = "0"+day; }
  return year + "-" + month + "-" + day;
}
/**获取用户信息，没有的话则存入到云数据库 */
function getAndSetUserInfo(callback=null){
  if(!(app.globalData.openid)){
    if(callback != null){
      callback("error");
    }
    return;
  }
  const _this = this;
  wx.cloud.callFunction({
    name: 'quickstartFunctions',
    data:{
      type:"getUserInfo",
      openid:app.globalData.openid
    },
    complete: res => {
      console.log('getAndSetUserInfo1: ', res);

      if(!res.result || !res.result.data || !res.result.data.length > 0){
        wx.cloud.callFunction({
          name: 'quickstartFunctions',
          data:{
            type:"setUserInfo",
            openid:app.globalData.openid,
            viplevel:0,
            orderTickets:[],
            orderHotels:[],
            orderShops:[]
          },
          complete: res => {
            console.log('getAndSetUserInfo2: ', res);//
            _this.getAndSetUserInfo();
          }
        })
      }
      else{
        app.globalData.userInfo = res.result.data[0];//
        console.log('getAndSetUserInfo33: ', app.globalData.userInfo);
        if(callback != null){
          callback("success");
        }
      }
    }
  })
}


module.exports = {
  gotoTestPage,
  gotoScenicDetailsPage,
  gotoShopAddressPage,
  gotoShopConsigneePage,
  gotoShopQuickPage,
  gotoShopDetailsPage,
  gotoOrderGoodsPage,
  gotoOrderTicketsPage,
  gotoOrderHotelsPage,
  gotoOrderShopsPage,
  getAndSetUserInfo,
  ////////////
  formatTime,
  gotoServiceTalk,
  gotoMyInfo,
  gotoMyRegister,
  gotoScenic,
  gotoScenicList,
  gotoTicketsList,
  gotoGiftPage,
  gotoHotelsListPage,
  gotoHotelDetails,
  gotoHotelBuyPage,
  gotoResttPage,
  gotoResttListPage,
  gotoAppreisePage,
  gotoCommentMePage,
  gotoCommentSubmitPage,
  gotoPlayGuidePage,
  gotoShopListPage,
  gotoTicketsBuyPage,
  gotoMapPage,
  sortArray,
  gotoShopSubmitPage,
  getDateStr
}
